$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/gzhbrandmap/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id',align: "center", width: 50, key: true},
			{ label: '公众号标识', name: 'gzhCode', index: 'gzh_code',align: "center", width: 80 },
			{ label: '公众号名称', name: 'gzhName', index: 'gzh_name',align: "center", width: 80 },
			{ label: '品牌名称', name: 'brandName', index: 'brand_name',align: "center", width: 80 },
			{ label: '品牌流量状态', name: 'isRandom', index: 'is_random',align: "center", width: 80, formatter:function(value,options,rowData) {
					if(value==0) {
						return "不随机";
					}else{
						return "随机";
					}
				}
			},
			{ label: '操作', name: '', index: '',align: "center", width: 80 ,formatter:function(value,options,rowData) {
					let id=rowData["id"];
					return "<button class='btn btn-danger' onclick=vm.del('"+id+"')>删除</button>";
				}
			}
        ],
		viewrecords: true,
		height: "100%",
		rowNum: 12,
		rowList : [12,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/buserinfo/getUserGzhList',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				//vm.bbrandinfo=r.brandInfo;
				$("#selectUserGzhList").append(
					"<option value=''>全部平台</option>");
				$("#gzhList").append(
					"<option value=''>全部平台</option>");
				for(var i=0;i<r.userGzhList.length;i++){
					$("#selectUserGzhList").append(
						"<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
					$("#gzhList").append(
						"<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
				}
			}
		}
	});
});

setTimeout(function () {
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/bbrandinfo/queryAll',
		contentType: "application/json",
		success: function(r){
			// console.log(r);
			if(r.code==0){
				for(var i=0;i<r.brandInfo.length;i++){
					$("#brandName").append(
						"<option value=" + r.brandInfo[i].brandName +">"+"</option>");
				}
				for(var i=0;i<r.brandInfo.length;i++){
					$("#coverBrand").append(
						"<option value=" + r.brandInfo[i].brandName +">"+"</option>");
				}
				$("#brandNames").scrollTop()
				$("#linkBrandName").scrollTop()
			}
		}
	});
},2000)

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			gzhCode: ''
		},
		showList: true,
		title: null,
		gzhBrandMap: {},
		bbrandinfo: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.gzhBrandMap = {};
			vm.gzhBrandMap.gzhCode = '';
			vm.gzhBrandMap.isRandom = 0;
			$("#linkBrandName").val("");
		},
		saveOrUpdate: function (event) {
			if (vm.gzhBrandMap.gzhCode == '') {
				alert("请选择公众号");
				return;
			}
			let brandName = $("#linkBrandName").val();
			if (brandName == "") {
				alert("请选择品牌");
				return;
			}
			$.ajax({
				type: "POST",
				url: baseURL + "sys/gzhbrandmap/save",
				contentType: "application/json;charset=UTF-8",
				data: JSON.stringify(vm.gzhBrandMap),
				dataType: "JSON",
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getBrandId: function (obj) {
			let brandName = $(obj).val();
			vm.bbrandinfo.brandName = brandName;

			$.ajax({
				type: "POST",
				url: baseURL+"sys/bbrandinfo/queryByName",
				contentType: "application/json;charset=utf-8",
				data: JSON.stringify(vm.bbrandinfo),
				dataType: "JSON",
				success: function (r) {
					if (r.code == 0 && r.brandInfo) {
						vm.gzhBrandMap.brandId = r.brandInfo.id;
					}
				}
			})
		},
		del: function (id) {
			if(id == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/gzhbrandmap/delete/"+id,
                    contentType: "application/json",
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		reload: function (event) {
			vm.showList = true;
			var brandName=$("#brandNames").val();
			var page = 1;
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'brandName':brandName,'gzhCode': vm.q.gzhCode},
                page:page
            }).trigger("reloadGrid");
		}
	}
});
